<?php

namespace App\Listeners;


use Overtrue\LaravelUEditor\Events\Uploaded;

class UeditorUploadedListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  Uploaded  $event
     * @return void
     */
    public function handle(Uploaded $event)
    {
        //上传完之后同步到oss上
        $result = $event->result;
        //$result['type'] 返回值始终是tmp，不知为何
        if($result['type'] == 'tmp') {
            $result['type'] = pathinfo($result['url'], PATHINFO_EXTENSION);
        }
        \Log::debug('uploaded event result : '.json_encode($result));
        if(isset($result['url'])){
            $bucketType = get_oss_bucket_type($result['url']);
            $bucket = config('site.qiniu.bucket')[$bucketType];
            if(!sync2qiniu($bucket,$result['url'], public_path($result['url']))){
                \Log::debug('uploaded event sync2oss failed -- '.$event->result['url']);
            }
        }
    }
}
